﻿#include"ProtectionCover.h"
#include"Articles.h"
#include<qtimer.h>

#include"qdebug.h"
ProtectionCover::ProtectionCover(Articles& parent, int time, int Hz)
	:m_parent(parent)
{
	int w = parent.getModelRect().width();
	int h = parent.getModelRect().height();
	int r = 0.5 * qMax(w,h);
	Initia(parent.getCenterPos(), r, 5);
	m_ProtectionTime.setInterval(time);
	m_ProtectionTime.callOnTimeout([this]() {closeProtect(); m_over = true; });
	startProtect(Hz);
}

ProtectionCover::~ProtectionCover()
{
}

void ProtectionCover::update()
{
	if(m_ProtectionTime.isActive() || m_pause)
	setPointO(m_parent);
}

void ProtectionCover::draw(QPainter* painter)
{
	if (m_ProtectionTime.isActive()|| m_pause)
	DrawMulticoloredCircle::draw(painter);
}

void ProtectionCover::setPointO(Articles& parent)
{
	DrawMulticoloredCircle::setPointO(parent.getCenterPos());
}

void ProtectionCover::startProtect()
{
	m_ProtectionTime.start();
	//setHz(10);
}

void ProtectionCover::startProtect(int Hz)
{
	m_ProtectionTime.start();
	setHz(Hz);
}

void ProtectionCover::closeProtect()
{
	m_ProtectionTime.stop();
}

bool ProtectionCover::ProtectOver()
{
	return m_over;
}

void ProtectionCover::setPause(bool flag)
{
	m_pause = flag;
	static int time = 0;
	static int Hz=0;
	if (flag)
	{
		int Time = getProtectRemainingTime();
		if(Time!=-1)
			time =Time ;
		Hz = getHz();
		//qDebug() << Hz;
		closeProtect();
		setHz(0);
	}
	else
	{
		m_ProtectionTime.start(time);
		setHz(Hz);
	}
}

bool ProtectionCover::getProtectState()
{
	return m_ProtectionTime.isActive();
}

void ProtectionCover::setProtectTime(int time)
{
	m_ProtectionTime.setInterval(time);
}

const int ProtectionCover::getProtectRemainingTime()
{
	std::lock_guard<std::mutex>uniqe(mtx);//加锁
	return m_ProtectionTime.remainingTime();
}
